package com.link2room.rodimus.facade.listener.receiver;

import com.link2room.common.constant.DefaultValue;
import com.link2room.common.enums.BaseSysExceptionEnum;
import com.link2room.common.exception.SysException;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.L2RAssert;
import com.link2room.rodimus.entity.SignInAwardBaseEntity;
import com.link2room.rodimus.entity.SignInAwardDrawDetailEntity;
import com.link2room.rodimus.factory.SignInDrawAwardHandlerFactory;
import com.link2room.rodimus.handler.ISignInDrawAwardHandler;
import com.link2room.rodimus.listener.event.SignInDistributeAwardEvent;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

import static com.link2room.enums.exception.RodimusSysExceptionEnum.SIGN_IN_DRAW_AWARD;
import static com.link2room.rodimus.entity.SignInRuleBaseEntity.SignInRuleBaseEntity_.code;

/**
 * Created by LN on 2018/1/11
 */
@Component
public class SignInDistributeAwardEventReceiver {
    @Resource
    private ICommonService commonService;
    @EventListener(value = SignInDistributeAwardEvent.class)
    public void SignInDistributeAwardEvent(SignInDistributeAwardEvent event){
        if(event == null ){
            return;
        }
        List<SignInAwardDrawDetailEntity> entities = event.getEntities();
        if(CollectionUtils.isEmpty(entities))return;
        entities.stream()
                .forEach(entity->{
                    String memberNo = entity.getMemberNo();
                    SignInAwardDrawDetailEntity qresD = null;
                    SignInAwardBaseEntity findAwardBase = new SignInAwardBaseEntity();
                    findAwardBase.setCode(entity.getAwardCode());
                    findAwardBase.setLhotelGroupCode(entity.getLhotelGroupCode());
                    SignInAwardBaseEntity awardBase = null;
                    try {
                        awardBase = commonService.findOnlyOneEQ(SignInAwardBaseEntity.class, findAwardBase);
                    } catch (Exception e) {
                        throw new SysException(BaseSysExceptionEnum.BASE_FUCATION_OPTION,e);
                    }
                    try {
                        qresD = commonService.findOneEQ(SignInAwardDrawDetailEntity.class, entity);
                    } catch (Exception e) {
                        throw new SysException(BaseSysExceptionEnum.BASE_FUCATION_OPTION,e);
                    }
                    L2RAssert.isTrue(null == qresD, new SysException(SIGN_IN_DRAW_AWARD, "没有领奖次数.[memberNo] " + memberNo + "[awardCode] " + code));
                    qresD.setHasDrawn(DefaultValue.T);
                    try {
                        commonService.merge(qresD);
                    } catch (Exception e) {
                        throw new SysException(BaseSysExceptionEnum.BASE_FUCATION_OPTION,e);
                    }

                    String type = awardBase.getType();
                    String value = awardBase.getValue();

                    ISignInDrawAwardHandler handler = SignInDrawAwardHandlerFactory.getHandler(type);
                    try {
                        handler.drawAward(entity.getLhotelGroupCode(), type, value);
                    } catch (Exception e) {
                        qresD.setHasDrawn(DefaultValue.F);
                        try {
                            commonService.merge(qresD);
                        } catch (Exception e1) {
                            throw new SysException(BaseSysExceptionEnum.BASE_FUCATION_OPTION,e);
                        }
                        throw new SysException(BaseSysExceptionEnum.BASE_FUCATION_OPTION,e);
                    }
                });
    }

}
